package com.ap.core.domain;

import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.hibernate.annotations.Type;

import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import java.io.Serializable;

/**
 * User: Ranjit.Singh
 * <T> Type for the ID</T>
 */
@MappedSuperclass
public abstract class Identifiable<T> implements Serializable{

    @Id
    protected T id;

    /**
     * Gets the id.
     *
     *
     * @return the id
     */


    @Type(type="id")
    public T getId() {
        return id;
    }

    /**
     * Sets the id.
     *
     * @param id
     *            the new id
     */
    public void setId(final T id) {
        this.id = id;
    }

    // - - - - - equals() and hashCode() - - - - - -

    /** {@inheritDoc}
     */
    @SuppressWarnings("unchecked")
    @Override
    public boolean equals(final Object obj) {
        boolean retVal = false;
        if (obj != null) {
            if (obj == this) {
                retVal = true;
            } else {
                retVal = obj.getClass() == getClass() && new EqualsBuilder().append(this.id, ((Identifiable<T>) obj).id).isEquals();
            }
        }
        return retVal;
    }

    /** {@inheritDoc}
     */
    @Override
    public int hashCode() {
        return new HashCodeBuilder().append(this.id).toHashCode();
    }


}
